<?php

namespace Modules\Auth\Http\Controllers;
use Modules\Auth\Http\Requests\UserRegisterRequest;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Modules\Common\Http\Controllers\CommonController;
use Modules\Auth\Services\UserService;
use Modules\Auth\Entities\User;
use Modules\Common\Transformers\CommonResource;

class UserController extends CommonController
{

    private $service;
    public function __construct(UserService $service) {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        $item = $this->queryBuilder(User::query())->aFilters([])->aIncludes([])->page();
        return $this->success(CommonResource::collection($item) );

    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('auth::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(UserRegisterRequest $request)
    {

        $result = $this->service->store();
        if(!$result){
            return $this->fail('注册失败！');
        }
        return $this->created( $result);
    }
    /**
     * 缓存前部用户
     */
    public function cacheUser(){
        return Cache::rememberForever('cacheUser', function() {
            return DB::table('users')->get();
        });
    }
    /**
     * 清理全部用户缓存
     */
    public function forget_User(){
        Cache::forget('cacheUser');
    }
    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {

        $item = $this->queryBuilder(User::query())->aFilters([])->aIncludes([])->page();
        return $this->success( $item);
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('auth::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}
